$(function() {
	"use strict";

	const forms = document.querySelectorAll('.needs-validation')

	// 验证
	Array.from(forms).forEach(form => {
		form.addEventListener('submit', event => {
		if (!form.checkValidity()) {
			event.preventDefault()
			event.stopPropagation()
		}

		form.classList.add('was-validated')
		}, false)
	})

	/// 获取文件后缀
	function getFileExtension(filename) {
		return filename.slice(filename.lastIndexOf('.') + 1);
	}

	// 显示提示信息，type：1表示成功消息，0表示失败消息
	function showToast(type, msg) {
		const toastLiveExample = document.getElementById('liveToast');
		const toast = new bootstrap.Toast(toastLiveExample);
		if (type == 1) {
			$("#toastSuccessMessage").html(msg);
		} else {
			$("#toastFailMessage").html(msg);
		}
		toast.show();
	}

	$('.fullscreen').on('click', function() {
        if (document.fullscreenEnabled) {
            if (!document.fullscreenElement) {
               document.documentElement.requestFullscreen();
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                }
            }
        } else {
            alert('Fullscreen not supported');
        }
    });

	// 侧边栏隐藏与显示
	$("#togglemenu").click(function() {
		var isHide = $(".dy-sidebar").hasClass("d-none");
		if (isHide) {
			$(".dy-sidebar").removeClass("d-none");
			$(".dy-main").css("margin-left", "16rem");
		} else {
			$(".dy-sidebar").addClass("d-none");
			$(".dy-main").css("margin-left", "0rem");
		}
	})

	// 点击侧边栏用户显示或隐藏用户菜单
	$("#toggleuser").click(function(){
		$(".dy-slidebar-user-menu").slideToggle();
	})

	// delete 
	$(".deleteResource").click(function() {
		var res = confirm("确定要删除么?");
		if (res) {
			var url = $(this).data("url");
			// 构建表单
			var form = document.createElement("form");
			form.style.display = "none";
			form.method = "post";
			form.action = url;
			document.body.appendChild(form);
			form.submit();
		}
	})

	// 当前菜单
	$("#navbar-menu").find("a").each(function() {
		var curHref = $(this).data("baseurl");
		var fullHref = window.location.pathname;
		// 去掉最后的斜杠后的内容
		var idx = fullHref.lastIndexOf("/");
		if (idx >= 0) {
		    fullHref = fullHref.substring(0, idx);
			if (fullHref === curHref) {
				$(this).addClass("active");
			}
		}
	})

	// flash消息
	var flashMessage = $.cookie("_flash");
	if (flashMessage != undefined && flashMessage != "") {
		if (flashMessage.indexOf("_flash_success") >= 0) {
			flashMessage = flashMessage.replace("_flash_success", "");
			showToast(1, flashMessage);
		} else {
			showToast(0, flashMessage);
		}
		$.cookie("_flash", "", {path:"/"});
	}

    $(".file_upload").change(function() {
		var file = $(this).get(0).files[0];
		var formData = new FormData();
		formData.method = "post";
		formData.append("file", file);
		$.ajax({
			url: "/backend/upload",
			type: "POST",
			data: formData,
			processData:false,
      		contentType:false,
			success: function(data) {
				if(data.code == 200) {
					$(".upload_res").find("input[type=hidden]").val(data.data.url);
					// 是否图片
					var ext = getFileExtension(data.data.url);
					if (ext != 'mp4' || ext != 'avi') {
						$(".show_img").html("<img src='" + data.data.full_url + "' width='200'/>");
					}
				} else {
					showToast(0, data.msg);
				}
			}
		})
	})
    
	// 富文本
	tinymce.init({
		selector: '#tiny_content',
		language:'zh_CN',
		plugins: 'link', 
		promotion: false,
		height: 500
	});
    
});